package com.bit.cfdi.web.vm.perfil;

import java.io.IOException;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.zkoss.bind.annotation.GlobalCommand;
import org.zkoss.bind.annotation.Init;
import org.zkoss.bind.annotation.NotifyChange;
import org.zkoss.image.AImage;
import org.zkoss.image.Image;
import org.zkoss.zk.ui.select.annotation.VariableResolver;
import org.zkoss.zk.ui.select.annotation.WireVariable;

import com.bit.cfdi.web.domain.Imagen;
import com.bit.cfdi.web.domain.Persona;
import com.bit.cfdi.web.domain.Usuario;
import com.bit.cfdi.web.services.ImagenService;
import com.bit.cfdi.web.session.SessionUtils;
import com.bit.cfdi.web.utils.ByteUtils;
import com.bit.cfdi.web.utils.ImageUtils;

@VariableResolver(org.zkoss.zkplus.spring.DelegatingVariableResolver.class)
public class NavPerfilViewModel {
	
	private Logger log = LogManager.getLogger(this.getClass().getName());
	
	//Variables from Spring
	@WireVariable private SessionUtils sessionUtils;
	@WireVariable private ImagenService imagenService;
	
	private Usuario usuario;
	private String nombreCompleto;
	private Image image;
	
	@Init
	public void init() {
		log.info("init called");
		getInfo();
	}
	
	//Getters - Setters
	public Usuario getUsuario() {
		return usuario;
	}
	
	public String getNombreCompleto() {
		return nombreCompleto;
	}
	
	public Image getImage() {
		return image;
	}
	
	//GlobalCommands
	@GlobalCommand @NotifyChange({"*"})
	public void refreshProfileNavInfo() {
		getInfo();
	}
	
	//Private methods
	public void getInfo() {
		usuario = (Usuario) sessionUtils.getFromSession(SessionUtils.USUARIO);
		Persona persona = usuario.getPersona();
		StringBuilder strb = new StringBuilder();
		strb.append(persona.getNombre()!=null ? 
				persona.getNombre() + " " : "");
		strb.append(persona.getApellidoPaterno()!=null ? 
				persona.getApellidoPaterno() + " " : "" );
		strb.append(persona.getApellidoMaterno()!=null ? 
				persona.getApellidoMaterno() : "");
		nombreCompleto = strb.toString();
		
		Imagen defaultImage = imagenService.getDefaultImage(persona);
		if(defaultImage == null) {
			image = getDefaultProfileIcon();
		}
		else {
			image = getProfileImage(defaultImage);
		}
	}
	
	private org.zkoss.image.Image getDefaultProfileIcon() {
		ClassLoader cl = getClass().getClassLoader();
		org.zkoss.zul.Image imageToSend = new org.zkoss.zul.Image();
		try {
			imageToSend.setContent(new AImage(cl.getResource("perfil/el.png")));
			return imageToSend.getContent();
		} catch (IOException e) {
			log.error("Error creating default profile pic");
			return null;
		}
	}
	
	private org.zkoss.image.Image getProfileImage(final Imagen imagen) {
		Byte[] storedImage = imagen.getImagenPerfil();
		String imageFormat = imagen.getFormat();
		try {
			return ImageUtils.scaleToSize(ByteUtils.toByteNative(storedImage), 
					50, 50, imageFormat).getContent();
		} catch (IOException e) {
			return getDefaultProfileIcon();
		}
	}

}
